/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-4-8
 * Time: 下午7:20
 * To change this template use File | Settings | File Templates.
 */
// 合同元素定义加载合同定义查询项
$.fn.htdycombobox = function (multi, htlx) {
    var option = {
        url: $.webPath() + '/htdygl/htdy/comboboxHtdyList?htlx=' + htlx,
        valueField: 'id',
        textField: 'mc',
        multiple: multi,
        panelHeight: '100'
    }

    this.combobox($.createArg(option, arguments));
}

// 合同模板加载
$.fn.htmbcombobox = function (multi, htlx) {
    var option = {
        url: $.webPath() + '/htdygl/htmb/comboboxHtmbList?htdyId=' + htlx,
        valueField: 'id',
        textField: 'htmbmc',
        multiple: multi,
        panelHeight: '100'
    }

    this.combobox($.createArg(option, arguments));
}

// 保存合同文本
function htwb_save(htType) {
    var htContent = "";
    var htdyId = $('#htdy_id').val();
    var htwbId = $('#htwb_id').val();
    var htId = "";
    if (htType == "cght") {
        htId = $("[name='cght.id']").val();
    }
    if (htType == "xsht") {
        htId = $("[name='xsht.id']").val();
    }

    if (htType == "bght") {
        htId = $("[name='bght.id']").val();
    }

    // 选择页面的所有input
    var $inputs = $("#page input");
    if ($inputs.size() > 0) {
        var $arrInput = $inputs.map(function () {
            if ($(this).attr("name")) {
                return $(this).attr("name") + ":o" + "<![CDATA[" + $(this).val() + "]]>" + ",o";
            }
        });

        htContent += $arrInput.get().join("");
    }


    var $textarea = $("#page textarea");
    if ($textarea.size() > 0) {
        var $arrTextarea = $textarea.map(function () {
            if ($(this).attr("name")) {
                return $(this).attr("name") + ":o" + "<![CDATA[" + $(this).val() + "]]>" + ",o";
            }
        });

        htContent += $arrTextarea.get().join("");
    }

    // 判断是否有数据
    if (!htContent || !htdyId || !htId) {
        $.messager.alert("提示", '没有数据不能进行保存', 'warn');
        return;
    }

    $.ajax({
        type: "POST",
        url: $.webPath() + "/htdygl/htwb/saveHtwb",
        dataType: "json",
        data: {htContent: htContent, htdyId: htdyId, htwbId: htwbId, htId: htId, htType: htType},
        beforeSend: function () {
            $.messager.progress();
        },
        success: function (data) {
            $.messager.progress("close");
            if (data.statusCode == 300)
                $.messager.alert("提示", data.message, 'error');

            if (data.statusCode == 200) {
                $.messager.alert('提示', data.message, 'success');
            }

        }
    });
}

// 保存合同模块
function htmb_save() {
    if (!$("#cght_mbname").val()) {
        alert("请填写合同模板名称!");
        return;
    }

    var htContent = "";
    var htdyId = $('#htdy_id').val();  //合同定义ID

    var htmbId = 0;  //合同模块ID

    //保存合同模块基础数据
    $.ajax({
        type: "POST",
        url: $.webPath() + "/htdygl/htmb/addOrUpdateHtmb",
        dataType: "json",
        async: false, //同步操作
        data: {htContent: htContent, htmb_htdy: htdyId, htmb_bz: "从合同中自动生成", htmb_mc: $("#cght_mbname").val()},
        beforeSend: function () {
            $.messager.progress();
        },
        success: function (data) {
            if (data.statusCode == 200) {
                htmbId = data.id;
            }
        }
    });
    if (htmbId == 0 || !htmbId) {
        $.messager.alert("提示", '保存合同基础信息失败', 'warn');
        $.messager.progress("close");
        return;
    }

    // 选择页面的所有input
    var inputElementsName = document.getElementById("page").getElementsByTagName("input");
    if (null != inputElementsName && inputElementsName.length > 0) {
        for (var i = 0; i < inputElementsName.length; i++) {
            if (null != inputElementsName[i].name) {
                htContent = htContent + inputElementsName[i].name + ":o" + "<![CDATA[" + inputElementsName[i].value + "]]>" + ",o";
            }
        }
    }

    // 选择页面的所有textarea
    var textareaElementsName = document.getElementById("page").getElementsByTagName("textarea");
    if (null != textareaElementsName && textareaElementsName.length > 0) {
        for (var i = 0; i < textareaElementsName.length; i++) {
            if (null != textareaElementsName[i].name) {
                htContent = htContent + textareaElementsName[i].name + ":o" + "<![CDATA[" + textareaElementsName[i].value + "]]>" + ",o";
            }
        }
    }

    // 判断是否有数据
    if (!htContent || !htdyId) {
        $.messager.alert("提示", '没有数据不能进行保存', 'warn');
        return;
    }
    $.ajax({
        type: "POST",
        url: $.webPath() + "/htdygl/htmb/saveHtmb",
        dataType: "json",
        data: {htContent: htContent, htmbId: htmbId, htdyId: htdyId},
        success: function (data) {
            $.messager.progress("close");
            if (data.statusCode == 300)
                $.messager.alert("提示", data.message, 'error');

            if (data.statusCode == 200) {
                $.messager.alert('提示', data.message, 'success');
            }

        }
    });
}

//  检查插件
function checkIsInstall(PRE) {
    try {
        var LODOP = getLodop(document.getElementById(PRE+'LODOP_OB'), document.getElementById(PRE+'LODOP_EM'));
        if ((LODOP != null) && (typeof(LODOP.VERSION) != "undefined"))
            alert("本机已成功安装过Lodop控件!\n  版本号:" + LODOP.VERSION);
    } catch (err) {
        alert("Error:本机未安装或需要升级!");
    }
};

// 打印预览.
function printPreview(PRE,htbm, emwtime) {
    var LODOP = getLodop(document.getElementById(PRE+'LODOP_OB'), document.getElementById(PRE+'LODOP_EM'));
    LODOP.SET_PRINT_STYLEA(0, "NotOnlyHighPrecision", true);
    LODOP.PRINT_INIT("预览合同");
    var inputs = document.getElementById("page").getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
        inputs[i].setAttribute("value", inputs[i].value);
    }
    var strBodyStyle = "<style  type=\"text/css\">" +
        ".textarea-tian,.input-tian{border-top:0 none;border-left:0 none;border-right:0 none;border-bottom: 1px solid black;height:22px;line-height:22px;width:100%;font-weight: bold;}" +
        " select{display:none}" +
        ".tb{width:auto;border-collapse:collapse;border-spacing:0;}" +
        ".tb th{padding:2px 2px 2px 12px;font-size:12px;font-weight:normal;text-align: right;}" +
        ".tb td{padding:2px;font-size:12px;}" +
        ".tb caption{ font-size:24px;height:60px;line-height:20px;}" +
        ".intb{width: 100%;}" +
        ".intb,.intb th,.intb td{border:1px solid black;}" +
        ".textarea-tian{height:100%;overflow-y:hidden}" +
        "</style>";
    var strFormHtml = strBodyStyle + "<div>" + document.getElementById("page").innerHTML + "</div>";
    LODOP.ADD_PRINT_HTM(40, 30, "85%", "90%", strFormHtml);
    //LODOP.ADD_PRINT_BARCODE(28, 550, 100, 100, 'QRCode', htbm + " " + emwtime);
//    LODOP.SET_PRINT_STYLEA(0, 'PageIndex', 'last');
//    LODOP.SET_PRINT_STYLEA(0, 'LinkedItem', 1);
//    LODOP.SET_PRINT_STYLEA(0, "QRCodeVersion", 3);

    LODOP.PREVIEW();
}

// 打印
function print(PRE,htbm, emwtime) {
    var LODOP = getLodop(document.getElementById(PRE+'LODOP_OB'), document.getElementById(PRE+'LODOP_EM'));
    LODOP.SET_PRINT_STYLEA(0, "NotOnlyHighPrecision", true);
    LODOP.PRINT_INIT("打印合同");
    LODOP.SET_PRINT_STYLE("FontSize", 18);

    var inputs = document.getElementById("page").getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
        inputs[i].setAttribute("value", inputs[i].value);
    }
    var strBodyStyle = "<style  type=\"text/css\">" +
        ".textarea-tian,.input-tian{border-top:0 none;border-left:0 none;border-right:0 none;border-bottom: 1px solid black;height:22px;line-height:22px;width:100%;font-weight: bold;}" +
        " select{display:none}" +
        ".tb{width:auto;border-collapse:collapse;border-spacing:0;}" +
        ".tb th{padding:2px 2px 2px 12px;font-size:12px;font-weight:normal;text-align: right;}" +
        ".tb td{padding:2px;font-size:12px;}" +
        ".tb caption{ font-size:24px;height:60px;line-height:20px;}" +
        ".intb{width: 100%;}" +
        ".intb,.intb th,.intb td{border:1px solid black;}" +
        ".textarea-tian{height:100%;overflow-y:hidden}" +
        "</style>";
    var strFormHtml = strBodyStyle + "<body>" + document.getElementById("page").innerHTML + "</body>";
    LODOP.SET_PRINT_MODE("PRINT_PAGE_PERCENT", "Auto-Width");
    LODOP.ADD_PRINT_HTM(40, 50, "85%", "90%", strFormHtml);
    LODOP.ADD_PRINT_BARCODE(28, 550, 100, 100, 'QRCode', htbm + " " + emwtime);
    LODOP.SET_PRINT_STYLEA(0, 'PageIndex', 'last');
    LODOP.SET_PRINT_STYLEA(0, 'LinkedItem', 1);
    LODOP.SET_PRINT_STYLEA(0, "QRCodeVersion", 3);
    LODOP.PRINT();
}
